home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / shells / rc-1.000 / rc-1 / rc-1.5-linux / rc-FAQ < prev    next >
Encoding:
Text File  |  1994-07-06  |  43.1 KB  |  1,106 lines

  1. Subject: rc shell Frequently Asked Questions
  2. Summary: Answers to Frequently Asked Questions about rc -- a clean,
  3.          portable, and freely-available shell for UNIX systems.
  4. Newsgroups: comp.unix.shell,comp.answers,news.answers
  5. From: alan@oldp.astro.wisc.edu (Alan Watson)
  6. Organization: Department of Astronomy, University of Wisconsin -- Madison
  7. Followup-to: poster
  8.  
  9. Archive-name: unix-faq/shell/rc
  10. Comp-unix-shell-archive-name: rc-FAQ
  11. Version: 2.18
  12. Last-modified: 05 Jul 1994
  13.  
  14. This document attempts to answer some Frequently Asked Questions about
  15. rc, a clean, portable, and freely-available shell for UNIX systems (see
  16. 1.1 for a very brief overview of rc).  It in no way replaces either the
  17. formal documentation for rc (see 1.2) or the mailing list archives (see
  18. 2.2).
  19.  
  20. This document draws heavily on the collective wisdom of the rc mailing
  21. list; the quality of the correspondence is such that often all that was
  22. required was a summary of one or two messages.  The rc mailing list
  23. archive is recommend reading for newcomers to rc (see 2.2).
  24. Contributors are acknowledged at the end of this document.
  25.  
  26. Corrections, comments, and suggestions concerning this document are
  27. welcome (see 3.2).
  28.  
  29. NOTE: Neither the editor nor any of the contributors to this document
  30. are responsible for any consequences resulting from the advice in this
  31. document.
  32.  
  33. Table of Contents
  34.  
  35. 1.      Implementations of rc
  36. 1.1     What is rc?
  37. 1.2     Where is rc documented?
  38. 1.3     How do I obtain rc?
  39. 1.4     Are there any patches to rc?
  40. 1.5     Can I compile rc on a ... machine with ... operating system and
  41.         ... compiler?
  42. 1.6     Should rc be dynamically or statically linked?
  43. 1.7     To whom do I report bugs?
  44. 1.8     Does anyone have an implementation of /dev/fd for my UNIX?
  45.  
  46. 2.      The rc mailing list
  47. 2.1     How do I subscribe to the rc mailing list?
  48. 2.2     How do I obtain the rc mailing list archive?
  49.  
  50. 3.      The rc FAQ list
  51. 3.1     How do I obtain the current version of the rc FAQ list?
  52. 3.2     To whom do I make suggestions about the rc FAQ list?
  53.  
  54. 4.      Does rc have ... ?
  55. 4.1     Does rc have job control?
  56. 4.2     Does rc have command-line editing?
  57. 4.3     Does rc have a history mechanism?
  58. 4.4     Does rc have file name completion?
  59. 4.5     Does rc have home directory expansion?
  60. 4.6     Does rc check for new mail?
  61. 4.7     Does rc have hierarchical lists?
  62. 4.8     Does rc have cartesian products?
  63. 4.9     Does rc cache the location of executables in the path?
  64. 4.10    Does rc have C-style \ escapes?
  65. 4.11    Does rc have ... built in?
  66. 4.12    Does rc have echo built in?
  67. 4.13    Does rc have test built in?
  68. 4.14    Does rc have true, false, and `:' built in?
  69.  
  70. 5.      Using rc as a login shell
  71. 5.1     How do I make rc my login shell?
  72. 5.2     How do I use rc in a heterogeneous cluster?
  73. 5.3     What problems occur with rc on Apollos?
  74. 5.4     What problems occur with rc on SYSV-derived systems?
  75. 5.5     What problems occur with rc on NeXTs?
  76. 5.6     How do I keep down the size of my .rcrc file?
  77. 5.7     How do I speed up the execution of my .rcrc file?
  78.  
  79. 6.      Interactions between rc and the rest of UNIX
  80. 6.1     How do I get each new interactive rc to do ...?
  81. 6.2     How do I get rc to do ... in each new xterm?
  82. 6.3     How do I get rc to read .rcrc when I use rsh?
  83. 6.4     How do I get at or batch to run an rc script?
  84.  
  85. 7.      Programming in rc
  86. 7.1     What makes rc such a good scripting language?
  87. 7.2     What are the trade-offs between functions and scripts?
  88. 7.3     What can I do if I have deficient sed, awk, test, ... ?
  89. 7.4     Where do I obtain the GNU tools and other software?
  90. 7.5     How do I perform arithmetic in rc?
  91. 7.6     How do I find the first occurrence of an element in a list?
  92. 7.7     How do I use ranges in array subscripts in rc?
  93. 7.8     How do I perform the equivalent of read in rc?
  94.  
  95. 1.      Implementations of rc
  96.  
  97. 1.1     What is rc?
  98.  
  99.         rc is a shell designed by Tom Duff of Bell Labs to replace the
  100.         venerable Bourne sh in Plan 9.  (Plan 9 is an experimental
  101.         operating and networking system developed at Bell Labs.)  In
  102.         addition to Plan 9, Duff's rc runs on SunOS and v10 UNIX.  The
  103.         design of rc is extremely clean, in that precisely those
  104.         features of Bourne's sh that have caused problems over the years
  105.         (the slightly awkward flow control syntax, the Byzantine quoting
  106.         rules, the failure to export all variables into the environment)
  107.         were reworked, and the temptation to add superfluous features
  108.         was strongly resisted.
  109.  
  110.         Byron Rakitzis has written a version of rc that is portable
  111.         between many version on UNIX and is largely compatible with
  112.         Duff's design.  Rakitzis' implementation has been successfully
  113.         used on UNIX platforms ranging from PCs running Linux to Cray
  114.         Y-MPs running Unicos.
  115.  
  116.         Most of this document deals with issues that have arisen from
  117.         use of Rakitzis' rc, and most of the example code will follow
  118.         his minor modifications to rc.  Henceforth, a plain `rc' refers
  119.         to Rakitzis' rather than Duff's rc.
  120.  
  121. 1.2     Where is rc documented?
  122.  
  123.         rc is documented in its man page, distributed with the source
  124.         (see 1.3).
  125.  
  126.         The Plan 9 manual pages, including those on Duff's rc, are
  127.         available as research.att.com:/dist/plan9man/manual.ps.Z by
  128.         anonymous FTP.  The full manual is almost 600 pages long, but
  129.         the manual pages for Duff's rc are physical pages 142 to 147
  130.         (logical pages 116 to 121).
  131.  
  132.         An early version of Duff's rc is described in the paper `rc -- a
  133.         Shell for Plan 9 and UNIX Systems', written for a UKUUG meeting.
  134.         Duff has kindly allowed the paper to be distributed by anonymous
  135.         FTP as viz.tamu.edu:/pub/rc/plan9.ps.  It provides an excellent
  136.         introduction and tutorial for Duff's rc, and describes the
  137.         motivations for the differences between Duff's rc and Bourne's
  138.         sh.
  139.  
  140.         When reading Duff's paper and the Plan 9 manual pages, bear in
  141.         mind that Rakitzis' implementation of rc differs in detail from
  142.         Duff's (these differences are documented in the rc man page).
  143.  
  144.         Another valuable source of information on rc is the rc mailing
  145.         list archive (see 2.2).
  146.  
  147. 1.3     How do I obtain rc?
  148.  
  149.         The sources are available by anonymous FTP in the directory
  150.         viz.tamu.edu:/pub/rc.  Be sure to get the most recent version.
  151.         At the time this document was last updated, the most recent
  152.         version of rc was v1.4 announced on 21 May 1992, available as
  153.         viz.tamu.edu:/pub/rc/rc-1.4.tar.Z.  (A beta release of v1.5 is
  154.         on the horizon.)
  155.  
  156.         rc is distributed on very liberal terms, although it has not
  157.         been placed in the public domain (see the file COPYRIGHT
  158.         distributed with the sources).
  159.  
  160.         Duff's rc can be obtained by acquiring either a Plan 9 or a v10
  161.         UNIX system from Bell Labs.  Plan 9 can be licensed by academic
  162.         institutions, which should contact:
  163.  
  164.             Neera Kuckreja
  165.             RM 2C557
  166.             ATT Bell Laboratories
  167.             Murray Hill
  168.             NJ 07974
  169.             USA
  170.             +1 908 582-3855
  171.             neera@research.att.com
  172.  
  173. 1.4     Are there any patches to rc?
  174.  
  175.         Although the current version of rc is remarkably mature, a small
  176.         number of bugs have been found since its release and patches
  177.         have been posted to the rc mailing list (see 2.2).  Other
  178.         patches posted to the mailing list include work-arounds for
  179.         broken compilers or libraries and assorted features.
  180.  
  181.         An up-to-date set of patches extracted from the mailing list
  182.         archive, together with a script to apply those patches, is
  183.         available as oldp.astro.wisc.edu:/pub/rc/patches.tar.Z by
  184.         anonymous FTP.
  185.  
  186. 1.5     Can I compile rc on a ... machine with ... operating system and
  187.         ... compiler?
  188.  
  189.         rc is written in portable ANSI C, but it can be compiled by many
  190.         older C compilers after passing the sources through unproto (get
  191.         ftp.win.tue.nl:/pub/unix/unproto4.shar.Z by anonymous FTP).
  192.  
  193.         If you use gcc, use v1.39 or later.
  194.  
  195.         rc has been successfully compiled on systems with the following
  196.         combinations of architectures, operating systems, and compilers,
  197.         although this list is probably not exhaustive:
  198.  
  199.             DEC VAX        Ultrix v3.x            gcc v1
  200.             DEC MIPS       Ultrix v4.[23]         gcc v2
  201.             DEC MIPS       Ultrix v4.[23]         cc          [a]
  202.             DEC AXP        OSF/1 v1.2             cc          [a][b]
  203.             DEC AXP        OSF/1 v1.2             cc -std1    [b]
  204.  
  205.             Sun SPARC      Solaris v2             gcc v2      [b][c]
  206.             Sun SPARC                             acc         [b][c]
  207.             Sun SPARC      SunOS v4.1.[123]       cc          [a][b][c]
  208.             Sun SPARC      SunOS v4.1.[123]       gcc v2      [b][c]
  209.             Sun SPARC      BSD                                [b][c]
  210.             Sun 68K        SunOS v4.1.1           gcc v2      [b][c]
  211.             Sun 68K        SunOS v3.5             gcc v1      [b][c]
  212.  
  213.             SGI Iris       Irix v[45].x           gcc v2
  214.  
  215.             IBM RS/6000    AIX v3.1.5             cc
  216.             IBM RS/6000    AIX v3.2.[013]         cc
  217.  
  218.             HP PA-RISC     HP-UX v8.0             gcc v2      [d][e]
  219.             HP PA-RISC     HP-UX v9.0             gcc v2      [d]
  220.             HP PA-RISC     OSF/1 v1.0.2           cc
  221.  
  222.             i386/i486      SCO-ODT SVR3           gcc v2      [d][e]
  223.             i386/i486      SCO-ODT SVR3           cc          [d][e]
  224.             i386           Minix v1.5                         [d]
  225.             i386/i486      NetBSD                 gcc v[12]
  226.             i386/i486      Linux                  gcc v2      [d]
  227.             i386/i486      386BSD r0.0            gcc         [j]
  228.             i386/i486      SCO Xenix SVR2.3x                  [f]
  229.  
  230.             NeXT           NeXT OS v2.x           gcc v[12]   [g]
  231.             NeXT           NeXT OS v3.0           gcc v2      [g]
  232.  
  233.             Apollo 68K     DomainOS v10.4 (BSD)   cc          [h]
  234.             Apollo 10000   DomainOS v10.4 (BSD)   cc          [h]
  235.  
  236.             Sony MIPS      NEWS/OS                cc          [a]
  237.             Sony 68K       NEWS/OS v4.0.1         cc          [a]
  238.  
  239.             Convex C1      ConvexOS v10.1         cc
  240.  
  241.             Pyramid 9810   OSx 5.1a (BSD)         cc -Xa      [i]
  242.  
  243.             Cray Y-MP      Unicos 6.1.7           scc         [e][j]
  244.  
  245.             Sequent        Dynix v3.2.0 (BSD)     gcc v2
  246.  
  247.             Apple 68K      A/UX v3.0              cc
  248.  
  249.             Encore MIPS    Umax v4.3              gcc v2
  250.  
  251.             Acorn ARM      iX v1.21a              cc
  252.  
  253.             Motorola 88K   System V R32V3         gcc v2
  254.             Motorola 88K   System V R32V3         cc          [a][k]
  255.  
  256.         Notes:
  257.  
  258.             [a] Sources pre-processed with unproto.
  259.             [b] See 1.6.
  260.             [c] Sun's yacc has a memory leak -- use another
  261.                 implementation such as GNU bison (see 7.4).
  262.             [d] Requires intervention by hand to get mksignal to work,
  263.                 because of of the non-standard signal.h file.
  264.             [e] See 4.13.
  265.             [f] See 5.4.
  266.             [g] See 5.5.
  267.             [h] See 5.3
  268.             [i] This compiler has no stdarg.h, but vararg.h sufficed.
  269.             [j] Minor modifications required to prototypes.
  270.             [k] Greenhills cc, used without any optimization.
  271.  
  272. 1.6     Should rc be dynamically or statically linked?
  273.  
  274.         As far as one can generalize, statically linked executables fork
  275.         faster than dynamically linked executables.  This suggests that
  276.         shells, which fork a great deal, should probably be statically
  277.         linked (i.e., you should not link them with shared libraries).
  278.         rc makes so little use of libc functions that static linking
  279.         results in only a small increase in the size of the executable
  280.         on disc.
  281.  
  282.         Versions of UNIX which support shared libraries include SVR4,
  283.         SunOS, Solaris, OSF/1, and Irix.
  284.  
  285. 1.7     To whom do I report bugs?
  286.  
  287.         Byron Rakitzis (byron@netapp.com) or the rc mailing list
  288.         (rc@hawkwind.utcs.toronto.edu).  Please check the documentation
  289.         before reporting bugs, and check the mailing list archive in
  290.         case the bug is already known.
  291.  
  292. 1.8     Does anyone have an implementation of /dev/fd for my UNIX?
  293.  
  294.         /dev/fd pseudo-devices allow a more reliable implementation of
  295.         non-linear pipelines (i.e., <{...}  and >{...}) than named
  296.         pipes, but they are not present in all versions of UNIX.
  297.  
  298.         An implementation of Ultrix v4.2 and later was posted to the
  299.         mailing list in March 1993 (see 2.2).
  300.  
  301.         An implementation for SunOS v4.1.1 is also available (although
  302.         it may well work with v4.1.2 and v4.1.3) by anonymous FTP as
  303.         ftp.cs.arizona.edu:/devfd.shar.
  304.  
  305.         An implementation for SunOS v4.1.2 is available by anonymous FTP
  306.         as emx.cc.utexas.edu:/pub/mnt/source/sun/devfd.shar.  (This is
  307.         packaged as a loadable module, so you do not have to rebuild
  308.         your kernel.)
  309.  
  310.         An implementation for NeXT OS 2.1 is available by anonymous FTP
  311.         as ftp.sys.toronto.edu:/pub/rc/devfd-next.shar.gz.
  312.  
  313.         You will, of course, need to rebuild your kernel to install
  314.         these pseudo-devices.
  315.  
  316. 2.      The rc mailing list
  317.  
  318. 2.1     How do I subscribe to the rc mailing list?
  319.  
  320.         Send mail to
  321.  
  322.             rc-request@hawkwind.utcs.toronto.edu.
  323.  
  324.         Chris Siebenmann maintains the mailing list, and can be reached
  325.         at the above address in the event of problems with the mailing
  326.         list or to unsubscribe.
  327.  
  328.         The rc mailing list represents hundreds of user-years of
  329.         experience with rc.  Clearly, it is a courtesy to the list and
  330.         to your own advantage to check the mailing list archive before
  331.         posting to the list, to benefit from the experience and mistakes
  332.         of those before you, and to prevent topics being continually
  333.         rehashed.
  334.  
  335. 2.2     How do I obtain the rc mailing list archive?
  336.  
  337.         The archive can be obtained by anonymous FTP.  At the moment, it
  338.         is split chronologically between the original and current
  339.         maintainers of the mailing list.
  340.  
  341.         Period              File
  342.         Jun 91 to Dec 91    viz.tamu.edu:/pub/rc/rc-list.1991.Z
  343.         Jan 92 to May 92    viz.tamu.edu:/pub/rc/rc-list
  344.         May 92 to present   ftp.sys.toronto.edu:/pub/rc/rc-list
  345.  
  346. 3.      The rc FAQ list
  347.  
  348. 3.1     How do I obtain the current version of the rc FAQ list?
  349.  
  350.         The current version of the rc FAQ list is archived automatically
  351.         and is available by anonymous FTP as
  352.         rtfm.mit.edu:/pub/usenet/comp.unix.shell/rc-FAQ.
  353.  
  354. 3.2     To whom do I make suggestions about the rc FAQ list?
  355.  
  356.         To its editor, Alan Watson (alan@oldp.astro.wisc.edu), or the rc
  357.         mailing list if you think your suggestion warrants a wider
  358.         audience.
  359.  
  360. 4.      Does rc have ... ?
  361.  
  362. 4.1     Does rc have job control?
  363.  
  364.         No.  This has been discussed at length in the rc mailing list.
  365.  
  366.         Now that personal workstations with window systems are common
  367.         place, the case for job control is less compelling.  However, if
  368.         you have to use rc on a traditional terminal, you may wish to
  369.         obtain the screens program from a GNU archive (see 7.4), the sm
  370.         program that was posted to alt.sources in mid-1992 (see 7.4), or
  371.         investigate shell escape mechanisms in vi and shell mode in
  372.         emacs.
  373.  
  374. 4.2     Does rc have command-line editing?
  375.  
  376.         If you wish.  Hooks are provided for the readline or editline
  377.         libraries which provide command-line editing.  The default is no
  378.         command-line editing.
  379.  
  380.         The readline library can be obtained from a GNU archive (see
  381.         7.4).
  382.  
  383.         The editline library is similar to the readline library, but is
  384.         a fraction of the size and offers fewer features.  The sources
  385.         are available as ftp.sys.toronto.edu:/pub/rc/editline.shar by
  386.         anonymous FTP).
  387.  
  388.         Alternatively, you can use fep, ile, atty, or Rk to implement
  389.         editing on a per-terminal basis.  These have been discussed in
  390.         on the mailing list at one time or another, and modifications to
  391.         ile have been posted there.
  392.  
  393. 4.3     Does rc have a history mechanism?
  394.  
  395.         Yes.  While, rc does not have a built-in csh-like history
  396.         mechanism, rather it is able to write interactive commands to a
  397.         file (see the history special variable in the man page),
  398.         allowing a history mechanism similar to the v8 `=' command to be
  399.         implemented as an external program (one such implementation is
  400.         supplied with the source of rc).
  401.  
  402.         In Plan 9, `:' was the history program (the name was probably
  403.         changed from the `=' used in v8 because `=' is a special
  404.         character in rc, and needs quoting if it refers to a command).
  405.         However, this form of history was apparently used so little that
  406.         the ability to write commands to a file was removed from Duff's
  407.         rc.  The history program distributed with the rc source is
  408.         invoked with `-'.
  409.  
  410.         Both editline and readline have command recall.
  411.  
  412. 4.4     Does rc have file name completion?
  413.  
  414.         If you wish.  Plain rc does not have file name completion (as it
  415.         obtains its command input with read), but the readline library
  416.         does.
  417.  
  418. 4.5     Does rc have home directory expansion?
  419.  
  420.         No.  This has been hotly debated on the rc mailing list, and a
  421.         patch to implement this was posted to the rc mailing list in
  422.         November 1992 (see 2.2 and 1.4).
  423.  
  424. 4.6     Does rc check for new mail?
  425.  
  426.         No.  However, you can make it do so with the following prompt
  427.         function:
  428.  
  429.             fn prompt { mail -e && echo You have mail. }
  430.  
  431.         Alternatively, consider using biff or xbiff.
  432.  
  433. 4.7     Does rc have hierarchical lists?
  434.  
  435.         No.  Each element of an rc list must be a string, and cannot be
  436.         another list.  Whenever a list appears inside another list, it
  437.         is expanded so that the outer list contains its elements.
  438.         Similarly, whenever two lists are adjacent, they are
  439.         concatenated to form a single list (with one exception).
  440.  
  441.         In rc terms, the arguments to a command form a list, even though
  442.         they may not have explicit `(' and `)' delimiters around them.
  443.         Hierarchical lists do not correspond well to the `char **argv'
  444.         model for arguments to UNIX commands, although simple lists do.
  445.  
  446.         The single exception to these rules are the arguments to the `~'
  447.         built-in command, which rc interprets as two lists -- a subject
  448.         list and a pattern list.  Normal expansion and concatenation is
  449.         performed on the two lists separately.  A consequence of this is
  450.         that the following command:
  451.  
  452.                 if ( ~ $1 () '' ) ...
  453.  
  454.         does not check if $1 is either the null list or the null string,
  455.         as normal list concatenation occurs on the pattern list leading
  456.         to a pattern list with one element -- the null string.
  457.  
  458. 4.8     Does rc have cartesian products?
  459.  
  460.         No.  If you really need them, they can be obtained in the
  461.         following manner:
  462.  
  463.             ; x = (a b c)
  464.             ; y = (1 2 3)
  465.             ; product = ()
  466.             ; for ( elem in $x ) product = ( $product $elem^$y )
  467.             ; echo $product
  468.             a1 a2 a3 b1 b2 b3 c1 c2 c3
  469.  
  470.         rc's concatenation operator works in a distributive manner if
  471.         one of the lists has only a single element and in a pairwise
  472.         manner if both lists have the same number of elements:
  473.  
  474.             ; echo a^( x y z )
  475.             ax ay az
  476.             ; echo ( a b c )^( x y z )
  477.             ax by cz
  478.  
  479. 4.9     Does rc cache the location of executables in the path?
  480.  
  481.         No.  This has been discussed on the mailing list.  As far as one
  482.         can generalize, searching the path is not a performance
  483.         bottleneck on most modern UNIX systems.
  484.  
  485. 4.10    Does rc have C-style \ escapes?
  486.  
  487.         No.  One of the most attractive features of rc is its single
  488.         quoting rule.
  489.  
  490.         If you need to embed control characters in your strings,
  491.         investigate tr and printf (see 7.3 if your system lacks the
  492.         latter).
  493.  
  494. 4.11    Does rc have ... built in?
  495.  
  496.         It is not by accident that rc has fewer built-in commands than
  497.         most shells other than the original Bourne shell.  The
  498.         underlying philosophy, eloquently summarized by Boyd Roberts, is
  499.         that a shell is there to run other programs, not to have other
  500.         programs built into it.  By having a well-defined role, rc has
  501.         remained small and simple, properties that lead to better
  502.         performance, fewer bugs, and a more thorough understanding of
  503.         the shell by its users.
  504.  
  505.         Most of the rc built-in commands perform functions that are
  506.         essential for a shell and cannot be performed by external
  507.         programs (e.g., cd, eval, limit, umask, wait).
  508.  
  509.         rc does include hooks for users to add new built-in commands,
  510.         and these have been used to add test, access, kill, read, and
  511.         even sync and haltsys commands (see the mailing list and the
  512.         examples file for more details).  Such additions are unsupported
  513.         and potentially unportable.
  514.  
  515. 4.12    Does rc have echo built in?
  516.  
  517.         If you wish.  While most of rc's built-in commands could not
  518.         easily be replaced by external commands, echo is the one
  519.         exception.  The justification is that echo is used so much that
  520.         providing it as a built-in command results in a significant gain
  521.         in performance, although this opinion is by no means universally
  522.         held.  By default, echo is built in.
  523.  
  524.         It has also been noted that rc's echo is more portable that
  525.         /bin/echo, as the v7 and SYSV implementations of echo differ in
  526.         the method that must be used to suppress the final newline.
  527.         (However, rc's echo differs from both, as it uses `--' to
  528.         indicate that all other arguments are to be echoed literally.)
  529.  
  530.         People who dislike the notion of a built-in echo command have
  531.         the option of excluding it from the compilation.  Alternatively,
  532.         the built-in echo can be subverted with the following function,
  533.         although this will not defeat an explicit call to `builtin
  534.         echo'.
  535.  
  536.             fn echo { /bin/echo $* }
  537.  
  538.         See also 7.3.
  539.  
  540. 4.13    Does rc have test built in?
  541.  
  542.         No.  You can use the built-in `~' command to match strings, and
  543.         fall back on /bin/expr or /bin/test for numeric comparisons and
  544.         /bin/test for querying the file system.
  545.  
  546.         If you are using rc on a system that lacks a stand-alone
  547.         /bin/test and /bin/[ and has test built into sh, the following
  548.         serves as an interim replacement:
  549.  
  550.             #! /bin/sh
  551.             `basename $0` ${1+"$@"}
  552.             # end-of-file
  553.  
  554.         This applies to Unicos and HP-UX v8.  Note that trip.rc, the
  555.         script provided with the rc sources to check basic functionality
  556.         after compilation, will fail unless a suitable test command
  557.         exists in the path.
  558.  
  559.         SCO UNIX implements /bin/test in precisely this way.  You can
  560.         almost certainly get better performance by using the GNU
  561.         implementation of /bin/test (see 7.4), although this is
  562.         obviously more important on an i386 machine than on a Cray.
  563.  
  564.         It is possible to get at the file system obliquely through the
  565.         globbing mechanism and the builtin cd command, and in some
  566.         circumstances these can be of use (see, for example, 5.7).  Such
  567.         speed hacks need to be carefully justified.
  568.  
  569.         See also 7.3.
  570.  
  571. 4.14    Does rc have true, false, and `:' built in?
  572.  
  573.         No.  The following functions are just as fast:
  574.  
  575.             fn true  { return 0 }
  576.             fn false { return 1 }
  577.             fn : { return 0 }
  578.  
  579.         Note that an indefinite loop in rc does not require the use of
  580.         an explicit `true' command:
  581.  
  582.             while () {
  583.                 ...
  584.             }
  585.  
  586. 5.      Using rc as a login shell
  587.  
  588. 5.1     How do I make rc my login shell?
  589.  
  590.         On most systems, you or the system administrator will have to
  591.         add a line to /etc/shells containing the full path to rc.  This
  592.         will need to be done on each machine on which you wish to use
  593.         rc.
  594.  
  595.         Next, use the chsh command to specify your new preference.  If
  596.         your machine is running Yellow Pages (YP), you will have to do
  597.         this on the YP server, otherwise you will have to do this on
  598.         each machine individually.  (To determine if your machine is
  599.         using YP, type `ypwhich -m passwd' that will either print the
  600.         name of your YP server or give an error if your machine is not
  601.         running YP.)  Often, you will also need to change your password
  602.         (using the yppasswd command) on the YP server to propagate the
  603.         new /etc/passwd entry contain your selected shell; changing your
  604.         password to itself will normally suffice.
  605.  
  606.         An alternative method is to invoke rc from your .profile file or
  607.         its equivalent, but this has so many drawbacks that I cannot
  608.         recommend it.
  609.  
  610. 5.2     How do I use rc in a heterogeneous cluster?
  611.  
  612.         There are two issues: finding the correct executable when you
  613.         login, and getting the correct path in scripts run using the #!
  614.         kernel hack.
  615.  
  616.         If you have root privileges on every machine on the cluster, by
  617.         far the simplest solution is to place a copy of rc in /bin and
  618.         add /bin/rc to /etc/shells on each machine.  Use `#! /bin/rc' in
  619.         shell scripts.
  620.  
  621.         Otherwise, use a sh wrapper around rc.  Create a shell script
  622.         called, say, rc-login, and make this your login shell.  The
  623.         purpose of this script is to set SHELL and PATH to point
  624.         appropriately according to the machine, and then to exec rc.
  625.  
  626.         The following script is suitable for adaptation:
  627.  
  628.             #! /bin/sh
  629.  
  630.             # determine OS
  631.             TMP=/tmp/$$
  632.             ( exec >$TMP ; echo OS=`uname -m` )
  633.             . $TMP
  634.             rm -f $TMP
  635.  
  636.             # determine location of rc executable
  637.             case $OS in
  638.             RISC)
  639.                PATH=$HOME/bin/mips:$PATH
  640.                SHELL=$HOME/bin/mips/rc
  641.                ;;
  642.             sun4c)
  643.                PATH=$HOME/bin/sparc:$PATH
  644.                SHELL=$HOME/bin/sparc/rc
  645.                ;;
  646.             alpha)
  647.                PATH=$HOME/bin/alpha:$PATH
  648.                SHELL=$HOME/bin/alpha/rc
  649.                ;;
  650.             *)
  651.                echo 1>&2 "WARNING: unknown architecture"
  652.                SHELL=/bin/sh
  653.                ;;
  654.             esac
  655.  
  656.             # determine if this is a login shell
  657.             case $SHELL in
  658.             */rc)
  659.               case $0 in
  660.               -*)
  661.                  FLAG=-l
  662.                  ;;
  663.               esac
  664.               ;;
  665.             esac
  666.  
  667.             export PATH
  668.             export SHELL
  669.  
  670.             exec $SHELL $FLAG ${1+"$@"}
  671.  
  672.             # end-of-file
  673.  
  674.         The dance to capture the output of uname is necessary because of
  675.         a bug whereby redirection does not work in the original v7 sh if
  676.         stdout is closed.  (This bug lives on in the Ultrix /bin/sh.)
  677.  
  678.         The easiest way to manage shell scripts is to keep a master copy
  679.         in a central directory, say $home/bin/share, and have slave
  680.         scripts in machine-specific directories which source the master
  681.         script.  For example, for a command called `foo' create the
  682.         following in each machine-specific directory:
  683.  
  684.             #! /full/path/to/machine/specific/rc
  685.             . $home/bin/share/foo $*
  686.             # end-of-file
  687.  
  688.         See 5.3 if your cluster contains Apollos.
  689.  
  690. 5.3     What problems occur with rc on Apollos?
  691.  
  692.         Apollos require the login shell to be a binary, so you cannot
  693.         use a sh script wrapper around rc.
  694.  
  695. 5.4     What problems occur with rc on SYSV-derived systems?
  696.  
  697.         Many SYSV-derived sh and ksh are `restricted' if the final
  698.         component of SHELL contains the letter `r'.  A restricted shell
  699.         will not allow you to cd, perform output redirection, set PATH,
  700.         or specify absolute paths to executables, meaning that many
  701.         shell scripts written in sh or ksh will fail.  This is also
  702.         applies to BSD-derived systems that also ship with a SYSV-
  703.         derived shell (for example, /bin/sh5 on Ultrix).
  704.  
  705.         One solution is to link rc to a file that does not contain the
  706.         letter `r', and use that file as your SHELL.  You can achieve
  707.         this automatically by placing the following commands in your
  708.         .rcrc:
  709.  
  710.             rcdir = `{ dirname $SHELL } {
  711.                 test -f $rcdir/RC || ln $rcdir/rc $rcdir/RC
  712.                 SHELL = $rcdir/RC
  713.             }
  714.  
  715.         Make sure when you install a new version of rc that you remove
  716.         the previous RC.
  717.  
  718.         This behaviour is also a potential problem if you use a sh
  719.         wrapper around rc as your login shell (see 5.2 and 6.3).  Make
  720.         sure that the name of the wrapper does not contain the letter
  721.         `r'.
  722.  
  723. 5.5     What problems occur with rc on NeXTs?
  724.  
  725.         The NeXT window system assumes that all shells are `job control'
  726.         shells and will place themselves in a new process group.
  727.         Contrary to this assumption, rc is not a job control shell and
  728.         so does not do this.  The result is that a signal to one (such
  729.         as an interrupt) will be sent to all.
  730.  
  731.         The `newpgrp' built-in command was provided to deal with this
  732.         problem.  You can use it to ensure that each new interactive rc
  733.         belongs to a new process group by using a prompt function to run
  734.         code in each new interactive instance of rc (see 6.1 and 6.2).
  735.         Other more elegant solutions (such as a wrapper around rc) are
  736.         precluded by the uncooperative and inflexible nature of the NeXT
  737.         window system.
  738.  
  739.         See also 6.1 and 6.2.
  740.  
  741. 5.6     How do I keep down the size of my .rcrc file?
  742.  
  743.         Given the nature of rc, the natural place to define functions is
  744.         in the .rcrc file.  However, as the number of functions grows,
  745.         it becomes more awkward to maintain them all in a single file.
  746.         One solution is to break out the functions into separate files
  747.         and automatically load them within .rcrc:
  748.  
  749.             fnlib = $home/fnlib
  750.             fn load { ~ $#* 1 && builtin . $fnlib/$1 }
  751.             builtin cd $fnlib
  752.             name = () for ( name in * ) load $name
  753.             builtin cd
  754.  
  755.         See also 5.7.
  756.  
  757. 5.7     How do I speed up the execution of my .rcrc file?
  758.  
  759.         If you have lots of functions, you may wish to use lazy function
  760.         loading:
  761.  
  762.             fnlib = $home/fnlib
  763.             fn load { ~ $#* 1 && builtin . $fnlib/$1 }
  764.             builtin cd $fnlib
  765.             name = () for ( name in * ) fn $name { load $0 && $0 $* }
  766.             builtin cd
  767.  
  768.         Often, though, a more substantial amount of time is spent
  769.         testing directories before they are added to a path: rc does not
  770.         have a built-in test command, so each test implies a fork and an
  771.         exec.  In some circumstances, you can replace the test with code
  772.         similar to the following:
  773.  
  774.             path = ()
  775.             dir = () for ( dir in ... )
  776.                 builtin cd $dir >[1=] >[2=] && path = ( $path $dir )
  777.             builtin cd
  778.  
  779.         See also 5.6 and 4.13.
  780.  
  781. 6.      Interactions between rc and the rest of UNIX
  782.  
  783. 6.1     How do I get each new interactive rc to do ...?
  784.  
  785.         Rather than set the `-l' flag and place the code in .rcrc, you
  786.         can use a prompt function like the following:
  787.  
  788.             fn prompt {
  789.                 if ( ! ~ $ipid $pid ) {
  790.                     ipid=$pid
  791.                     ...
  792.                 }
  793.             }
  794.  
  795.         This will run ... at the start of each new interactive instance
  796.         of rc, but not in non-interactive instances of rc.
  797.  
  798.         See also 6.2 and 5.5.
  799.  
  800. 6.2     How do I get rc to do ... in each new xterm?
  801.  
  802.         Since xterm puts WINDOWID into the environment, you can use a
  803.         prompt function like the following:
  804.  
  805.             fn prompt {
  806.                 if ( ! ~ $wid $WINDOWID && ~ $TERM xterm* ) {
  807.                     wid=$WINDOWID
  808.                     ...
  809.                 }
  810.             }
  811.  
  812.         See also 6.1.
  813.  
  814. 6.3     How do I get rc to read .rcrc when I use rsh?
  815.  
  816.         rshd does not place a `-' in the first character of argv[0] when
  817.         it execs a shell, so .rcrc is not run and this can cause
  818.         problems.
  819.  
  820.         One solution is to have a false login shell called login-shell
  821.         that explicitly supplies a -l flag to rc, i.e., login-shell
  822.         contains:
  823.  
  824.             #! /path/to/rc
  825.             exec /path/to/rc -l $*
  826.             # end-of-file
  827.  
  828.         See also 5.2.
  829.  
  830. 6.4     How do I get at or batch to run an rc script?
  831.  
  832.         at and batch only accept sh or csh scripts, so you need to
  833.         package your rc script in a sh wrapper.  The following two
  834.         functions accomplish this.  Note that they do not completely
  835.         emulate the standard commands, as they will not accept a file
  836.         name on the command line, but rather only read commands from
  837.         stdin.  A simple further enhancement might be to mail the stdout
  838.         and stderr of the job back to the user.
  839.  
  840.             fn at {
  841.                 if ( ~ $#* 0 ) {
  842.                     echo >[2=1] \
  843.                         'usage: at [ time [day] | -r job ... | -l [job ...] ]'
  844.                     return 1
  845.                 }
  846.                 switch ( $1 ) {
  847.                 case -r
  848.                     builtin at $*
  849.                 case -l
  850.                     builtin at $* >[2=1]
  851.                 case *
  852.                     {
  853.                         echo 'SHELL='^$SHELL
  854.                         echo 'export SHELL'
  855.                         echo 'sed ''s/^-//'' <<''+'' | '^$SHELL
  856.                         sed 's/^/-/'
  857.                         echo '+'
  858.                     } | SHELL = /bin/sh builtin at $*
  859.                 }
  860.             }
  861.  
  862.             fn batch {
  863.                 if ( ! ~ $#* 0 ) {
  864.                     echo >[1=2] 'usage: batch'
  865.                     return 1
  866.                 }
  867.                 {
  868.                     echo 'SHELL='^$SHELL
  869.                     echo 'export SHELL'
  870.                     echo 'sed ''s/^-//'' <<''+'' | '^$SHELL
  871.                     sed 's/^/-/'
  872.                     echo '+'
  873.                 } | SHELL = /bin/sh builtin batch
  874.             }
  875.  
  876. 7.      Programming in rc
  877.  
  878. 7.1     What makes rc such a good scripting language?
  879.  
  880.         Brian Kernighan and Rob Pike wrote of the original v7 Bourne
  881.         shell:
  882.  
  883.             The UNIX shell isn't typical of command interpreters:
  884.             although it lets you run commands in the usual way, because
  885.             it is a programming language it can accomplish much more.
  886.  
  887.         Their comments apply equally to rc, perhaps more so.
  888.  
  889.         The two aspects of rc that contribute most to its utility as a
  890.         scripting language are its simple quoting rule coupled with the
  891.         absence of rescanning and the fact that the language is small,
  892.         simple, and well defined.
  893.  
  894.         The former allows one to write scripts with complete control
  895.         over the lexical aspects of the process: when and if globbing,
  896.         pattern matching, and list formation take place.  Tom Duff wrote
  897.         that rc is `not a macro processor'.  The lack or rescanning,
  898.         together with improvements to backquote substitutions, allowed
  899.         him to collapse Bourne's four types of quoting down to one,
  900.         eliminating all of their complex interactions.
  901.  
  902.         The latter means that the whole language can be readily
  903.         understood by the user.  Its grammar is well defined, in sharp
  904.         contrast to sh.  (Indeed, a skeleton yacc grammar is included as
  905.         an appendix to rc's manual page.)  The language is small and
  906.         well defined; there are, to quote John Mackin, no `dark
  907.         corners'.  Although the language is smaller and simpler, it
  908.         retains all the power and flexibility of sh, and improves and
  909.         extends on it in several areas.
  910.  
  911. 7.2     What are the trade-offs between functions and scripts?
  912.  
  913.         Functions are run in the process that invokes them, so they can
  914.         make persistent changes to the environment of the interpreter.
  915.         They are essential for some actions (reading a variable, for
  916.         example).
  917.  
  918.         Functions make excellent wrappers around standard commands, as
  919.         one can use the `builtin' command to invoke the standard command
  920.         from within the function.  Invoking a standard command from
  921.         within a script of the same name involves either hard-wiring the
  922.         name into the script or using a more elaborate method, such as
  923.         the `pathos' script posted to the mailing list in June 1992.
  924.  
  925.         There is less overhead in invoking a function, as the kernel
  926.         need not exec rc once again.
  927.  
  928.         However, functions take up space in the environment.  There is
  929.         often a hard limit on the size of the environment.  Although on
  930.         some systems this limit is very large, on others it is not.  In
  931.         addition, very large environments can degrade the performance of
  932.         fork.
  933.  
  934.         Functions cannot be exec-ed directly by a C program, but must be
  935.         invoked through rc, although it is probably a good idea to do
  936.         this anyway, as responsibility for globbing and variable
  937.         substitution properly resides with the shell.
  938.  
  939. 7.3     What can I do if I have deficient sed, awk, test, ... ?
  940.  
  941.         Shell programming on UNIX rests on two foundations: the shell
  942.         itself and the toolkit of programs provided in /bin and
  943.         /usr/bin.  Unfortunately, many vendors provide out-of-date tools
  944.         and there are many incompatibilities between BSD- and SYSV-
  945.         derived systems, so writing portable scripts is a problem.  This
  946.         problem can be especially acute in heterogeneous clusters.
  947.  
  948.         There are two approaches to this problem: one is to use only
  949.         those tools and options that were present in v7, and the other
  950.         is to use only those tools and options that are present in
  951.         POSIX.2, and to replace the vendor-supplied tools with those
  952.         from GNU if the former do not conform (see 7.4).
  953.  
  954. 7.4     Where do I obtain the GNU tools and other software?
  955.  
  956.         Get prep.ai.mit.edu:/pub/gnu/GNUinfo/FTP by anonymous FTP for a
  957.         list of sites that archive GNU software.
  958.  
  959.         The article `How to find sources' lists sites that archive
  960.         software posted to Usenet source newgroups.  This article is
  961.         regularly cross-posted to comp.sources.wanted, comp.answers, and
  962.         news.answers, and is also available via anonymous FTP as
  963.         rtfm.mit.edu:/pub/usenet/comp.answers/finding-sources.
  964.  
  965. 7.5     How do I perform arithmetic in rc?
  966.  
  967.         You can use /bin/expr, /bin/awk, or unary arithmetic with lists
  968.         (see 7.6 for an example).
  969.  
  970. 7.6     How do I find the first occurrence of an element in a list?
  971.  
  972.         Use an `index' function, as in:
  973.  
  974.             ; x = ( a b a b )
  975.             ; index b $x
  976.             2
  977.  
  978.         This implementation of index uses a list to count up to the
  979.         index of the element:
  980.  
  981.             fn index {
  982.                 switch ( $#* ) {
  983.                 case 0 1
  984.                     echo >[1=2] 'usage: index pattern list ...'
  985.                     return 1
  986.                 case *
  987.                     elem = $1 prefix = () {
  988.                         shift
  989.                         if ( ! ~ $elem $* ) {
  990.                             echo 0
  991.                             return 1
  992.                         }
  993.                         while ( prefix = ( $prefix $1 ) && ! ~ $elem $1 )
  994.                             shift
  995.                         echo $#prefix
  996.                         return 0
  997.                     }
  998.                 }
  999.             }
  1000.  
  1001. 7.7     How do I use ranges in array subscripts in rc?
  1002.  
  1003.         Use a `seq' function, as in:
  1004.  
  1005.             ; x = ( a b c d e f g h )
  1006.             ; echo $x( `{seq 2 5} )
  1007.             b c d e
  1008.  
  1009.         There are many implementations of seq, most of which use awk,
  1010.         like this one:
  1011.  
  1012.             fn seq {
  1013.                 switch ( $#* ) {
  1014.                 case 1
  1015.                     * = ( 1 $1 )
  1016.                 case 2
  1017.                 case *
  1018.                     echo >[1=2] usage: seq [start] stop
  1019.                     return 1
  1020.                 }
  1021.                 echo $1 $2 | awk '{
  1022.                     if ( $1 <= $2 )
  1023.                         for ( i = $1; i <= $2; i++ ) print i;
  1024.                     else
  1025.                         for ( i = $1; i >= $2; i-- ) print i;
  1026.                 }'
  1027.             }
  1028.  
  1029. 7.8     How do I perform the equivalent of read in rc?
  1030.  
  1031.         Use a `read' function, to read a single line from stdin, remove
  1032.         the trailing newline, and assign it to a variable:
  1033.  
  1034.             ; read foo
  1035.             Do the funky gibbon!
  1036.             ; echo $foo
  1037.             Do the funky gibbon!
  1038.             ; echo $#foo
  1039.             1
  1040.  
  1041.         The following implementation of read returns 1 if EOF is seen,
  1042.         like the read in the v7 sh:
  1043.  
  1044.             fn read {
  1045.                 if ( ! ~ $#* 1 ) {
  1046.                     echo >[1=2] 'usage: read variable'
  1047.                     return 1
  1048.                 }
  1049.                 if ( ~ $1 '*' () *'='* ) {
  1050.                     echo >[1=2] 'read: bad variable name'
  1051.                     return 1
  1052.                 }
  1053.                 lines = () nl = () {
  1054.                     ifs = () { nl = `{ echo } }
  1055.                     ifs = $nl { lines = `{ line ; echo $status } }
  1056.                     if ( ~ $#lines 2 ) {
  1057.                         $1 = $lines(1)
  1058.                         return 0
  1059.                     } else {
  1060.                         $1 = ''
  1061.                         return $lines(1)
  1062.                     }
  1063.                 }
  1064.             }
  1065.  
  1066.         If your UNIX does not have a line command, the following is an
  1067.         straightforward substitute.
  1068.  
  1069.             #include <unistd.h>
  1070.             int main ()
  1071.             {
  1072.                 unsigned char c;
  1073.  
  1074.                 while (read (0, &c, 1) == 1 && c != '\n')
  1075.                     write (1, &c, 1);
  1076.                 write (1, "\n", 1);
  1077.                 return c != '\n';
  1078.             }
  1079.  
  1080.         Attempts to use standard UNIX utilities (e.g., "sed 1q", "head
  1081.         -1", and "awk '{ print; exit; }'") to replace the line command
  1082.         fail because line does not buffer its input.  This was discussed
  1083.         extensively in the mailing list in September 1991 (see 2.2).
  1084.  
  1085.         Alternatively, a patch to add a built-in read was posted to the
  1086.         mailing list in September 1993 (see 1.4).
  1087.  
  1088. Acknowledgements
  1089.  
  1090. Many people have contributed ideas, code, and information to this
  1091. document, either directly or indirectly, through the rc mailing list and
  1092. its influence on my understanding of rc.
  1093.  
  1094. People who have directly contributed to this document include Vincent
  1095. Broman, Raymond Chen, Tom Culliton, Stefan Dalibor, Jim Davis, Matt Day,
  1096. Matthew Farwell, David Fiander, Tim Goodwin, Paul Haahr, Charles Hannum,
  1097. David Hogan, Noel Hunt, John LoVerso, Hamish Macdonald, John Mackin,
  1098. Byron Rakitzis, Boyd Roberts, Rich Salz, Chris Siebenmann, Emin Gun
  1099. Sirer, Icarus Sparry, Gerry Tomlinson, Malte Uhl, Christopher Vance, and
  1100. Victor Zandy.  My apologies to anyone I have missed.
  1101.  
  1102. As a user of rc, I am indebted to Tom Duff for its design, to Steve
  1103. Bourne for its foundation, and to Byron Rakitzis for its implementation.
  1104.  
  1105. Alan Watson (alan@oldp.astro.wisc.edu)
  1106.